package com.gxa.service.user.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.common.exception.RRException;
import com.gxa.common.utils.CommonUtils;
import com.gxa.entity.user.SysRole;
import com.gxa.entity.user.SysUser;
import com.gxa.service.user.SysRoleService;
import com.gxa.mapper.user.SysRoleMapper;
import com.gxa.service.user.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author Administrator
* @description 针对表【sys_role(角色表)】的数据库操作Service实现
* @createDate 2022-10-17 15:08:42
*/
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
    implements SysRoleService{

    @Autowired
    private SysRoleMapper roleMapper;

    @Autowired
    private SysUserService userService;

    @Override
    public List<SysRole> selectRoleListByUsername(String username) {
        List<SysRole> roleList = null;
        try {
            roleList =this.roleMapper.selectRoleListByUsername(username);
        }catch (Exception e){
            throw  new RRException("拉取角色信息失败");
        }
        return roleList;
    }

    @Override
    public IPage<SysRole> selectRoleWithPage(Integer currentPage,
                                          Integer pageSize, String name) {
        IPage<SysRole> roleIPage = null;
        try {
            Page<SysRole> page = new Page<>(currentPage,pageSize);
            roleIPage = this.baseMapper.selectRoleWithPage(page, name);
        }catch (Exception e){
            e.printStackTrace();
            throw  new RRException("拉取角色信息失败");
        }
        return roleIPage;
    }

    @Override
    public void updateRoleMenu(String[] menuIds, String roleId) {
        // 1 先删除用户原有的角色
        this.baseMapper.deleteRoleMenusByRoleId(roleId);
        // 2 再去更新本次赋予的角色
        this.baseMapper.insertRoleMenu(menuIds,roleId);
    }

    @Override
    public boolean saveOrUpdateRole(SysRole role) {
        boolean isAdd = false;
        SysUser currentUser = this.userService.getCurrentUser();
        if(StringUtils.isBlank(role.getId())){
            role.setId(CommonUtils.getUUID());
            this.createEntity(role,currentUser);
            this.updateEntity(role,currentUser);
            this.save(role);
            isAdd = true;
        }else{
            this.updateEntity(role,currentUser);
            this.updateById(role);
        }
        return isAdd;
    }

    @Override
    public void deleteRoleById(String id) {
        // 当前角色所属用户
        List<SysUser> user = this.userService.findRoleWithUser(id);
        if(user.size() >0){
            throw new RRException("该角色已被用户绑定，不能删除");
        }
        try {
            //1 先删除角色
            this.baseMapper.deleteById(id);
            // 2 删除角色菜单关联信息
            this.baseMapper.deleteRoleMenusByRoleId(id);
        }catch (Exception ex){
            throw new RRException("删除角色信息失败!");
        }
    }

    @Override
    public List<SysRole> selectRoleByMenuId(String id) {
        List<SysRole> roleList = null;
        try {
            roleList =this.roleMapper.selectRoleByMenuId(id);
        }catch (Exception e){
            throw  new RRException("查询菜单绑定角色信息失败");
        }
        return roleList;
    }


    public void updateEntity(SysRole role, SysUser currentUser) {
        if (null != role) {
            role.setUpdator(currentUser.getId());
            role.setUpdatorName(currentUser.getUsername());
        }
        role.setUpdateTime(new Date());
    }

    public void createEntity(SysRole role,SysUser currentUser) {
        if (null != role) {
            role.setCreator(currentUser.getId());
            role.setCreatorName(currentUser.getUsername());
        }
        role.setCreateTime(new Date());
    }
}




